package com.match.competition_role.helper;

import com.alibaba.fastjson.JSONObject;
import com.match.competition_role.entity.CompetitionPermission;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 根据权限数据构建登录用户左侧菜单数据
 * </p>
 *
 */
public class MemuHelper {

    /**
     * 构建菜单
     * @param treeNodes
     * @return
     */
    public static List<JSONObject> bulid(List<CompetitionPermission> treeNodes) {
        List<JSONObject> meuns = new ArrayList<>();
        if(treeNodes.size() == 1) {
            CompetitionPermission topNode = treeNodes.get(0);
            //左侧一级菜单
            List<CompetitionPermission> oneMeunList = topNode.getChildren();
            for(CompetitionPermission one :oneMeunList) {
                JSONObject oneMeun = new JSONObject();
                oneMeun.put("path", one.getPath());
                oneMeun.put("component", one.getComponent());
                oneMeun.put("name", one.getRedirect());
                oneMeun.put("title",one.getPermissionName());
                oneMeun.put("icon",one.getIcon());
                oneMeun.put("states",one.getStates());
                oneMeun.put("hidden", one.getHidden());

//                JSONObject oneMeta = new JSONObject();
//                oneMeta.put("title", one.getPermissionName());
//                oneMeta.put("icon", one.getIcon());
//                oneMeun.put("meta", oneMeta);

                List<JSONObject> children = new ArrayList<>();
                List<CompetitionPermission> twoMeunList = one.getChildren();
                for(CompetitionPermission two :twoMeunList) {
                    JSONObject twoMeun = new JSONObject();
                    twoMeun.put("path", two.getPath());
                    twoMeun.put("component", two.getComponent());
                    twoMeun.put("name",two.getRedirect());
                    twoMeun.put("title",two.getPermissionName());
                    twoMeun.put("icon",two.getIcon());
                    twoMeun.put("states",two.getStates());
                    twoMeun.put("hidden", two.getHidden());

//                    JSONObject twoMeta = new JSONObject();
//                    twoMeta.put("title", two.getPermissionName());
//                    twoMeta.put("icon",two.getIcon());
//                    twoMeun.put("meta", twoMeta);

                    children.add(twoMeun);

                    List<CompetitionPermission> threeMeunList = two.getChildren();
                    for(CompetitionPermission three :threeMeunList) {
                        if(StringUtils.isEmpty(three.getPath())) continue;

                        JSONObject threeMeun = new JSONObject();
                        threeMeun.put("path", three.getPath());
                        threeMeun.put("component", three.getComponent());
                        threeMeun.put("name", three.getRedirect());
                        threeMeun.put("title", three.getPermissionName());
                        threeMeun.put("icon", three.getIcon());
                        threeMeun.put("states",three.getStates());
                        threeMeun.put("hidden", three.getHidden());

//                        JSONObject threeMeta = new JSONObject();
//                        threeMeta.put("title", three.getPermissionName());
//                        threeMeta.put("icon", three.getIcon());
//                        threeMeun.put("meta", threeMeta);

                        children.add(threeMeun);
                    }
                }
                oneMeun.put("children", children);
                meuns.add(oneMeun);
            }
        }
        return meuns;
    }
}
